package com.huawei.spider.cnki.util;

/*import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocalFileSystem;
import org.apache.hadoop.fs.Path;*/

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/****
 * @Author: liminghui
 * @Date: 2019/8/12 18:31
 * @Version: 1.0
 * @Description:
 **/
public class FileUtils {
    /*//判断是本地还是hdfs文件系统,存在就删除
    public static void deleteDir(Path path, Configuration conf) throws URISyntaxException, IOException, InterruptedException {
        if (path.toString().startsWith("hdfs://")) {
            FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), conf, "root");
            if (fileSystem.exists(path)) {
                fileSystem.delete(path, true);
            }
        } else {
            LocalFileSystem localFileSystem = FileSystem.getLocal(conf);
            if (localFileSystem.exists(path)) {
                //递归删除文件夹
                localFileSystem.delete(path, true);
            }
        }
    }*/

   /* //删除crc后缀的文件方法
    public static void deleteEndWithCRC(Path path) throws IOException {
        //判断是否是file:///
        String pathStr = path.toString();
        if (pathStr.startsWith("file:/")) {
            pathStr = pathStr.substring("file:/".length());
        }
        File file = new File(pathStr);
        List<File> fileList = getFileList(file);
        System.out.println(fileList);
        for (File file1 : fileList) {
            // System.out.println(file1.getName());
            if (file1.getName().contains(".crc") || file1.getName().contains("_SUCCESS")) {
                file1.delete();
            }
        }
    }*/

    //利用Queue队列实现文件夹中的文件递归遍历
    public static List<File> getFileList(File file) throws IOException {
        List<File> list = new ArrayList<File>();//存放所有的文件对象集合

        Queue<File> queue = new LinkedList<File>();//queue队列:FIFO
        queue.add(file);//C:\\Users\\86159\\Desktop\\download

        while (queue.size() > 0 && queue != null) {
            File file1 = queue.poll();//弹出  //\\download
            if (file1.isDirectory()) {
                //是文件夹,遍历文件孩子子对象数组并放入队列
                File[] files2 = file1.listFiles();//\download\\user
                for (File file2 : files2) {
                    queue.add(file2);//添加到队列尾部
                }
            } else if (file1.isFile()) {
                //是文件就加入List文件对象集合
                list.add(file1);
                //System.out.println(file1.getAbsolutePath());
            }
        }
        return list;
    }

    public static void main(String[] args) throws IOException {
        String pathStr = "C:\\Users\\ASUS\\Desktop\\spider";
        File file = new File(pathStr);
        file.delete();
        List<File> fileList = getFileList(file);
        for (File file1 : fileList) {
            System.out.println(file1);
        }
    }
}
